#include "DataTransform.h"

gaeaway_load::EquipmentOfInput DataTransformCLR::transformToEquipmentOfInput(LoadingWK::EquipmentClr^ pEquipment)
{
    gaeaway_load::EquipmentOfInput geaEquip;
    if (nullptr == pEquipment) { return geaEquip; }

    geaEquip.id = pEquipment->_id;
    geaEquip.length = pEquipment->_length;
    geaEquip.width = pEquipment->_width;
    geaEquip.height = pEquipment->_height;

    geaEquip.gravity.y = pEquipment->_gravityY;
    geaEquip.weight = pEquipment->_weight;

    Generic::List<LoadingWK::AxleLoadClr^>^ axleLoadList = pEquipment->_axleLoadList;
    if (axleLoadList != nullptr)
    {
        for (int j = 0; j < axleLoadList->Count; j++)
        {
            LoadingWK::AxleLoadClr^ pAxleLoad = axleLoadList[j];
            if (pAxleLoad == nullptr) { continue; }

            gaeaway_load::AxleLoad axleLoad;
            axleLoad.offsetenth = pAxleLoad->_offsetenth;
            axleLoad.weight = pAxleLoad->_weight;
            geaEquip.axleLoadList.emplace_back(axleLoad);
        }
    }

    Generic::List<Int32>^ airplaneTypeIDs = pEquipment->_adaptedAirplaneTypeIDs;
    if (airplaneTypeIDs != nullptr)
    {
        for (int i = 0; i < airplaneTypeIDs->Count; i++)
        {
            geaEquip.adaptedAirplaneTypeIDs.emplace_back(airplaneTypeIDs[i]);
        }
    }

    return geaEquip;
}

gaeaway_load::EquipmentsOfInput DataTransformCLR::transformToEquipmentsOfInput(Generic::List<LoadingWK::EquipmentClr^>^ pEquipmentListClr)
{
    gaeaway_load::EquipmentsOfInput equipments;
    if (pEquipmentListClr != nullptr)
    {
        for (int i = 0; i < pEquipmentListClr->Count; i++)
        {
            if (pEquipmentListClr[i] == nullptr){   continue;}
            equipments.emplace_back(DataTransformCLR::transformToEquipmentOfInput(pEquipmentListClr[i]));
        }
    }
    return equipments;
}

LoadingWK::EquipmentClr^ DataTransformCLR::transformToLoadingEquipment(const gaeaway_load::EquipmentOfInput& geaEquip)
{
    LoadingWK::EquipmentClr^ pEquipment = gcnew LoadingWK::EquipmentClr();
    pEquipment->_id = geaEquip.id;
    pEquipment->_length = geaEquip.length;
    pEquipment->_width = geaEquip.width;
    pEquipment->_height = geaEquip.height;

    pEquipment->_gravityY = geaEquip.gravity.y;
    pEquipment->_weight = geaEquip.weight;

    for (auto axleLoad : geaEquip.axleLoadList)
    {
        LoadingWK::AxleLoadClr^ pAxleLoad = gcnew LoadingWK::AxleLoadClr(axleLoad.offsetenth, axleLoad.weight);

        pEquipment->_axleLoadList->Add(pAxleLoad);
    }
    return pEquipment;
}

gaeaway_load::Airplane DataTransformCLR::transformToAirplane(LoadingWK::AirplaneClr^ pAirplane)
{
    gaeaway_load::Airplane airplaneT;
    airplaneT.typeID = pAirplane->_typeID;
    airplaneT.length = pAirplane->_length;
    airplaneT.width = pAirplane->_width;
    airplaneT.height = pAirplane->_height;

    airplaneT.gravity.y = pAirplane->_gravityY;
    airplaneT.minY = pAirplane->_minY;
    airplaneT.maxY = pAirplane->_maxY;
    airplaneT.weight = pAirplane->_weight;
    airplaneT.maxLoadWeight = pAirplane->_maxLoadWeight;
    airplaneT.locationTypeOfStartLoading = pAirplane->_locationTypeOfStartLoading;

    Generic::List<LoadingWK::WeightLimitClr^>^ pWeightLimitList = pAirplane->_weightLimitOfAreaList;
    for (int i = 0; i < pWeightLimitList->Count; i++)
    {
        LoadingWK::WeightLimitClr^ pWeight = pWeightLimitList[i];

        gaeaway_load::WeightLimitOfArea wla;
        wla.minValue = pWeight->_leftY;
        wla.maxValue = pWeight->_rightY;
        wla.maxLoadingWeight = pWeight->_maxLoadingWeight;
        airplaneT.weightLimitList.emplace_back(wla);
    }
    return airplaneT;
}

gaeaway_load::AirplaneList DataTransformCLR::transformToAirplaneList(Generic::List<LoadingWK::AirplaneClr^>^ pAirplaneListClr)
{
    gaeaway_load::AirplaneList airplaneList;
    if (nullptr == pAirplaneListClr){   return airplaneList;}

    for (int i = 0; i < pAirplaneListClr->Count; i++)
    {
        LoadingWK::AirplaneClr^ pAirClr = pAirplaneListClr[i];
        if (nullptr == pAirClr){   continue;}
        airplaneList.emplace_back(DataTransformCLR::transformToAirplane(pAirClr));
    }
    return airplaneList;
}

gaeaway_load::LoadedAirplane DataTransformCLR::transformToLoadedAirplane(LoadingWK::LoadedAirplaneClr^ pLoadedAirplane)
{
    return gaeaway_load::LoadedAirplane(
        DataTransformCLR::transformToAirplane(pLoadedAirplane), 
        pLoadedAirplane->_loadedIndex);
}

LoadingWK::LoadedAirplaneClr^ DataTransformCLR::transformToLoadedAirplaneClr(const gaeaway_load::LoadedAirplane& loadAir)
{
    LoadingWK::LoadedAirplaneClr^ pLoadedAirClr = gcnew LoadingWK::LoadedAirplaneClr();
    pLoadedAirClr->_typeID = loadAir.typeID;
    pLoadedAirClr->_loadedIndex = loadAir.loadedIndex;
    pLoadedAirClr->_length = loadAir.length;
    pLoadedAirClr->_width = loadAir.width;
    pLoadedAirClr->_height = loadAir.height;
    pLoadedAirClr->_gravityY = loadAir.gravity.y;
    pLoadedAirClr->_minY = loadAir.minY;
    pLoadedAirClr->_maxY = loadAir.maxY;
    pLoadedAirClr->_weight = loadAir.weight;
    pLoadedAirClr->_maxLoadWeight = loadAir.maxLoadWeight;
    pLoadedAirClr->_locationTypeOfStartLoading = loadAir.locationTypeOfStartLoading;

    for (auto limitWe : loadAir.weightLimitList)
    {
        LoadingWK::WeightLimitClr^ pWeigthLimt = gcnew LoadingWK::WeightLimitClr();
        pWeigthLimt->_maxLoadingWeight = limitWe.maxLoadingWeight;
        pWeigthLimt->_leftY = limitWe.minValue;
        pWeigthLimt->_rightY = limitWe.maxValue;
        pLoadedAirClr->_weightLimitOfAreaList->Add(pWeigthLimt);
    }
    for (auto equipment : loadAir.loadedEquipments)
    {
        LoadingWK::LoadedEquipmentClr^ pLoadedEquipment = gcnew LoadingWK::LoadedEquipmentClr();
        pLoadedEquipment->_idOfEquipment = equipment.equipmentID;
        pLoadedEquipment->_leftY_Equipment = equipment.leftYOfequipment;

        pLoadedEquipment->_typeIDOfAirplane = loadAir.typeID;
        pLoadedEquipment->_indexOfLoadedAirplane = loadAir.loadedIndex;
        pLoadedAirClr->_loadedEquipments->Add(pLoadedEquipment);
    }
    return pLoadedAirClr;
}

System::Collections::Generic::List<LoadingWK::LoadedAirplaneClr^>^ 
    DataTransformCLR::transformToLoadedAirplaneClrs(const gaeaway_load::LoadedAirplanes& loadedAirs)
{
    Generic::List<LoadingWK::LoadedAirplaneClr^>^ pLoadedAirplaneList = gcnew Generic::List<LoadingWK::LoadedAirplaneClr ^>(0);
    
    for (size_t index = 0 ; index < loadedAirs.size() ; index++)
    {
        pLoadedAirplaneList->Add(DataTransformCLR::transformToLoadedAirplaneClr(loadedAirs[index]));
    }
    return pLoadedAirplaneList;
}
